package com.sz.app.edi.rest.api;

import com.sz.app.edi.rest.api.dto.EdiUserAccountDto;
import com.sz.app.edi.service.constants.EdiCommCodes;
import com.sz.common.base.cache.redis.RedisPoolManager;
import com.sz.common.base.dto.ResultDto;
import com.sz.common.base.i18n.I18nMessages;
import com.sz.common.base.logger.Logger;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import redis.clients.jedis.Jedis;

import java.util.UUID;

@RestController
@RequestMapping("${api}/auth")
@Api(description = " ", tags = "002、安全登录")
public class SecurityController extends EdiBaseController {

    @Autowired
    protected I18nMessages messageSource;

    protected Logger logger = Logger.getLogger(this.getClass());

    @RequestMapping(value = "/tokenlogin", method = RequestMethod.POST)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "token", value = " \"token\":\"4512f247-d446-4523-b249-d7a6f7d092ae\"",
                    required = true, dataType = "String", paramType = "header")})
    public ResultDto login(@RequestHeader String token) throws Exception {
        EdiUserAccountDto dto = getUserInfoByToken(token);
        checkLogin(token);
        ResultDto result = new ResultDto(0, "login successed");
        result.setData(dto);
        return result;
    }


    @RequestMapping(value = "/tokenlogout", method = RequestMethod.POST)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "token", value = " \"token\":\"4512f247-d446-4523-b249-d7a6f7d092ae\"",
                    required = true, dataType = "String", paramType = "header")})
    public ResultDto logout(@RequestHeader String token) throws Exception {
        ResultDto dto = new ResultDto();
        // 仅退出已登录用户
        if (SecurityUtils.getSubject().isAuthenticated()) {
            // 进行用户的退出，给出提示信息
            SecurityUtils.getSubject().logout();

            Jedis jedis = RedisPoolManager.getJedis();
            byte[] sessionObj = jedis.hget("shiro-activeSessionCache-customer".getBytes(), token.getBytes());
            if (sessionObj == null) {
                sessionObj = jedis.hget("shiro-activeSessionCache-supplier".getBytes(), token.getBytes());
                if (sessionObj == null) {
                    jedis.hdel("shiro-activeSessionCache-customer".getBytes(), token.getBytes());
                }
            } else {
                jedis.hdel("shiro-activeSessionCache-customer".getBytes(), token.getBytes());
            }
            jedis.close();
            dto.setData("您已安全退出");
        } else {
            dto.setErrorCode(401);
            dto.setMsg("You have not been login");
            return dto;
        }
        return dto;
    }

}
